﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer.App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Teleport : TargetSpell
    {
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (!(TargetCreatureBase is Character))
            {
                myCreatureBase.SendMessage("You may only teleport other players!", "server");
                myCreatureBase.PlaySound("Ding", myCreatureBase.MapX, myCreatureBase.MapY);

                return;
            }

            int mapId = 0;
            int mapX = 0;
            int mapY = 0;
            string destination = "";

            if (spellIcon.Name.IndexOf("Renatus") > -1)
            {
                mapId = 11;
                mapX = 44;
                mapY = 9;
                destination = "Renatus";
            }
            else if (spellIcon.Name.IndexOf("Lake Shire") > -1)
            {
                mapId = 6;
                mapX = 9;
                mapY = 18;
                destination = "Lake Shire";
            }

            if (TargetCreatureBase != myCreatureBase)
            {
                PendingTeleportRequest teleport = new PendingTeleportRequest((Character)TargetCreatureBase, mapId, mapX, mapY,
                    myCreatureBase.Name + " wishes to teleport you to " + destination + ". \n\nDo you accept?");
            }
            else
                Managers.GameHandler.Teleport((Character)TargetCreatureBase, mapId, mapX, mapY, preServerTransferMethod);
        }

        private void preServerTransferMethod()
        {
            Character character = (Character)TargetCreatureBase;

            //Try to stop the round timer, this will only return true, if it was running, and was stopped
            //This means they are perma busy until they hit the other server,
            //If we return false, they are not busy due to lag or something, set them busy before transfer.
            if (character.tryStopRoundTimer() == false)
                character.SetBusy(true);
        }
    }
}
